package de.jmda.util.gui.swing;

import java.awt.EventQueue;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.Arrays;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;

import de.jmda.gui.swing.mvc.ControllerContextProvider;
import de.jmda.gui.swing.mvc.ControllerSupportEnabled;

@ControllerSupportEnabled
public class JPanelFavourites
    extends JPanel
    implements ControllerContextProvider, PropertyChangeListener
{
	/**
	 * Value is used in <code>package-info</code> and defines the XML namespace
	 * for this and other JAXB classes in this package.
	 */
	public final static String XML_NAMESPACE = "http://code.google.com/p/jmda/gui";

	/** Creates new form JPanelFavourites */
	private JPanelFavourites()
	{
		initComponents();
	}

	/**
	 * Constructor with model instance parameter for this component.
	 * 
	 * @param jPanelFavouritesModel model instance
	 * @param currentDirectory current directory
	 */
	public JPanelFavourites(
			JPanelFavouritesModel jPanelFavouritesModel, File currentDirectory)
	{
		this();
		controller.refreshControllerContext();
		controller.setCurrentDirectory(currentDirectory);
		controller.setModel(jPanelFavouritesModel);
	}

	@Override
  public void propertyChange(PropertyChangeEvent evt)
  {
		controller.propertyChange(evt);
  }

	public boolean addChangeDirectoryListener(
			ChangeDirectoryListener changeDirectoryListener)
	{
		return controller.addChangeDirectoryListener(changeDirectoryListener);
	}
	
	public boolean removeChangeDirectoryListener(
			ChangeDirectoryListener changeDirectoryListener)
	{
		return controller.removeChangeDirectoryListener(changeDirectoryListener);
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {
    java.awt.GridBagConstraints gridBagConstraints;

    jScrollPaneFavourites = new javax.swing.JScrollPane();
    jListFavourites = new javax.swing.JList();
    jPanelButtonBar = new javax.swing.JPanel();
    jButtonUp = new javax.swing.JButton();
    jButtonDown = new javax.swing.JButton();
    jButtonInsert = new javax.swing.JButton();
    jButtonRemove = new javax.swing.JButton();
    jButtonChangeDirectory = new javax.swing.JButton();

    setBorder(javax.swing.BorderFactory.createTitledBorder("Favourites"));
    setLayout(new java.awt.GridBagLayout());

    jListFavourites.setModel(new javax.swing.AbstractListModel() {
      String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
      public int getSize() { return strings.length; }
      public Object getElementAt(int i) { return strings[i]; }
    });
    jListFavourites.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
    jListFavourites.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
      public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
        jListFavouritesValueChanged(evt);
      }
    });
    jScrollPaneFavourites.setViewportView(jListFavourites);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.weighty = 1.0;
    add(jScrollPaneFavourites, gridBagConstraints);

    jPanelButtonBar.setLayout(new java.awt.GridBagLayout());

    jButtonUp.setText("up");
    jButtonUp.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButtonUpActionPerformed(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    jPanelButtonBar.add(jButtonUp, gridBagConstraints);

    jButtonDown.setText("down");
    jButtonDown.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButtonDownActionPerformed(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    jPanelButtonBar.add(jButtonDown, gridBagConstraints);

    jButtonInsert.setText("insert");
    jButtonInsert.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButtonInsertActionPerformed(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    jPanelButtonBar.add(jButtonInsert, gridBagConstraints);

    jButtonRemove.setText("remove");
    jButtonRemove.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButtonRemoveActionPerformed(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    jPanelButtonBar.add(jButtonRemove, gridBagConstraints);

    jButtonChangeDirectory.setText("change directory");
    jButtonChangeDirectory.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButtonChangeDirectoryActionPerformed(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.gridwidth = 2;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    jPanelButtonBar.add(jButtonChangeDirectory, gridBagConstraints);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    add(jPanelButtonBar, gridBagConstraints);
  }// </editor-fold>//GEN-END:initComponents

	private void jListFavouritesValueChanged(javax.swing.event.ListSelectionEvent evt)//GEN-FIRST:event_jListFavouritesValueChanged
	{//GEN-HEADEREND:event_jListFavouritesValueChanged
		controller.listSelectionValueChanged(evt);
	}//GEN-LAST:event_jListFavouritesValueChanged

	private void jButtonUpActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonUpActionPerformed
	{//GEN-HEADEREND:event_jButtonUpActionPerformed
		controller.upActionPerformed();
	}//GEN-LAST:event_jButtonUpActionPerformed

	private void jButtonDownActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonDownActionPerformed
	{//GEN-HEADEREND:event_jButtonDownActionPerformed
		controller.downActionPerformed();
	}//GEN-LAST:event_jButtonDownActionPerformed

	private void jButtonRemoveActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonRemoveActionPerformed
	{//GEN-HEADEREND:event_jButtonRemoveActionPerformed
		controller.removeActionPerformed();
	}//GEN-LAST:event_jButtonRemoveActionPerformed

	private void jButtonInsertActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonInsertActionPerformed
	{//GEN-HEADEREND:event_jButtonInsertActionPerformed
		controller.insertActionPerformed(evt);
	}//GEN-LAST:event_jButtonInsertActionPerformed

	private void jButtonChangeDirectoryActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonChangeDirectoryActionPerformed
	{//GEN-HEADEREND:event_jButtonChangeDirectoryActionPerformed
		controller.changeDirectoryActionPerformed(evt);
	}//GEN-LAST:event_jButtonChangeDirectoryActionPerformed

	/**
	 * @param args the command line arguments
	 */
	public static void main(final String args[])
	{
		try
		{
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}
		catch (Exception e)
		{
			// no exception expected with system look and feel
		}

		EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				JFrame jFrame =
				    new JFrame("test window for " + JPanelFavourites.class.getName());

				jFrame
				    .setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

				jFrame.add(
						new JPanelFavourites(
								new JPanelFavouritesModel(
										Arrays.asList(
												new File("1"), new File("2"), new File("3"))),
								new File(".")));

				jFrame.pack();
				jFrame.setVisible(true);
			}
		});
	}

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton jButtonChangeDirectory;
  private javax.swing.JButton jButtonDown;
  private javax.swing.JButton jButtonInsert;
  private javax.swing.JButton jButtonRemove;
  private javax.swing.JButton jButtonUp;
  private javax.swing.JList jListFavourites;
  private javax.swing.JPanel jPanelButtonBar;
  private javax.swing.JScrollPane jScrollPaneFavourites;
  // End of variables declaration//GEN-END:variables

	//GEN-BEGIN:controller support
	@de.jmda.gui.swing.mvc.ControllerSupportDisabled                     
	private de.jmda.util.gui.swing.JPanelFavouritesController controller =                       
			new de.jmda.util.gui.swing.JPanelFavouritesController(this);                             
                                                                         
	public de.jmda.gui.swing.mvc.ControllerContext getControllerContext()
	{                                                                      
		return new de.jmda.util.gui.swing.JPanelFavouritesControllerContext(
				this,
				jButtonChangeDirectory,
				jButtonDown,
				jButtonInsert,
				jButtonRemove,
				jButtonUp,
				jListFavourites,
				jPanelButtonBar,
				jScrollPaneFavourites);                             
	}                                                                      
	//GEN-END:controller support
}